/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Article_spec接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Article_specRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using System.Text;
using NetCoreCMS.Common;
using System.Collections.Generic;
using System.Linq;
using System.Data;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Article_specRepository:BaseRepository<Article_spec,Int32>, IArticle_specRepository
    {
        public Article_specRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }

        public bool Delete(int parent_id, string id_list)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("delete from [" + nameof(Article_spec) + "] where parent_id=" + parent_id + " and id not in(" + id_list + ")") > 0;
            }
        }

        public bool DeleteAll(int id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("delete from [" + nameof(Article_spec) + "] where  parent_id=@parent_id;delete from [" + nameof(Article_spec) + "] where  id=@id", new { parent_id = id, id = id }) > 0;
            }
        }

        public List<Article_spec> GetCategorySpecList(int category_id)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select S.* FROM [" + nameof(Article_spec) + "] as S INNER JOIN [" + nameof(Article_category_spec) + "] as C");
                strSql.Append(" ON S.id=C.spec_id and C.category_id=" + category_id);
                strSql.Append(" order by sort_id asc,S.id desc");
                return _dbConnection.Query<Models.Article_spec>(strSql.ToString()).ToList();
            }
        }

        public bool UpdateField(int id, string strValue)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                return _dbConnection.Execute("update [" + nameof(Article_spec) + "] set " + strValue + " where id=@id", new { id = id }) > 0;
            }
        }
        #region 私有方法================================
        /// <summary>
        /// 查找不存在的规格值并删除
        /// </summary>
        private void DeleteValues(List<Models.Article_spec_value> models, int parentId)
        {
            using (IDbConnection _dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString))
            {
                StringBuilder idList = new StringBuilder();
                if (models != null)
                {
                    foreach (Models.Article_spec_value modelt in models)
                    {
                        if (modelt.id > 0)
                        {
                            idList.Append(modelt.id + ",");
                        }
                    }
                    string id_list = Utils.DelLastChar(idList.ToString(), ",");
                    if (!string.IsNullOrEmpty(id_list))
                    {
                        _dbConnection.Execute("delete from [" + nameof(Article_spec) + "] where parent_id=" + parentId + " and id not in(" + id_list + ")");
                    }
                }
            }
        }
        #endregion
    }
}